<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>events.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_testing_events_events.js.html">events.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2008 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Event Simulation.
<a name="line17"></a> *
<a name="line18"></a> * Utility functions for simulating events at the Closure level. All functions
<a name="line19"></a> * in this package generate events by calling goog.events.fireListeners,
<a name="line20"></a> * rather than interfacing with the browser directly. This is intended for
<a name="line21"></a> * testing purposes, and should not be used in production code.
<a name="line22"></a> *
<a name="line23"></a> * The decision to use Closure events and dispatchers instead of the browser&#39;s
<a name="line24"></a> * native events and dispatchers was conscious and deliberate. Native event
<a name="line25"></a> * dispatchers have their own set of quirks and edge cases. Pure JS dispatchers
<a name="line26"></a> * are more robust and transparent.
<a name="line27"></a> *
<a name="line28"></a> * If you think you need a testing mechanism that uses native Event objects,
<a name="line29"></a> * please, please email closure-tech first to explain your use case before you
<a name="line30"></a> * sink time into this.
<a name="line31"></a> *
<a name="line32"></a> * @author nicksantos@google.com (Nick Santos)
<a name="line33"></a> */
<a name="line34"></a>
<a name="line35"></a>goog.provide(&#39;goog.testing.events&#39;);
<a name="line36"></a>goog.provide(&#39;goog.testing.events.Event&#39;);
<a name="line37"></a>
<a name="line38"></a>goog.require(&#39;goog.events&#39;);
<a name="line39"></a>goog.require(&#39;goog.events.BrowserEvent&#39;);
<a name="line40"></a>goog.require(&#39;goog.events.BrowserEvent.MouseButton&#39;);
<a name="line41"></a>goog.require(&#39;goog.events.BrowserFeature&#39;);
<a name="line42"></a>goog.require(&#39;goog.events.EventTarget&#39;);
<a name="line43"></a>goog.require(&#39;goog.events.EventType&#39;);
<a name="line44"></a>goog.require(&#39;goog.events.KeyCodes&#39;);
<a name="line45"></a>goog.require(&#39;goog.events.Listenable&#39;);
<a name="line46"></a>goog.require(&#39;goog.object&#39;);
<a name="line47"></a>goog.require(&#39;goog.style&#39;);
<a name="line48"></a>goog.require(&#39;goog.userAgent&#39;);
<a name="line49"></a>
<a name="line50"></a>
<a name="line51"></a>
<a name="line52"></a>/**
<a name="line53"></a> * goog.events.BrowserEvent expects an Event so we provide one for JSCompiler.
<a name="line54"></a> *
<a name="line55"></a> * This clones a lot of the functionality of goog.events.Event. This used to
<a name="line56"></a> * use a mixin, but the mixin results in confusing the two types when compiled.
<a name="line57"></a> *
<a name="line58"></a> * @param {string} type Event Type.
<a name="line59"></a> * @param {Object=} opt_target Reference to the object that is the target of
<a name="line60"></a> *     this event.
<a name="line61"></a> * @constructor
<a name="line62"></a> * @extends {Event}
<a name="line63"></a> */
<a name="line64"></a>goog.testing.events.Event = function(type, opt_target) {
<a name="line65"></a>  this.type = type;
<a name="line66"></a>
<a name="line67"></a>  this.target = /** @type {EventTarget} */ (opt_target || null);
<a name="line68"></a>
<a name="line69"></a>  this.currentTarget = this.target;
<a name="line70"></a>};
<a name="line71"></a>
<a name="line72"></a>
<a name="line73"></a>/**
<a name="line74"></a> * Whether to cancel the event in internal capture/bubble processing for IE.
<a name="line75"></a> * @type {boolean}
<a name="line76"></a> * @suppress {underscore} Technically public, but referencing this outside
<a name="line77"></a> *     this package is strongly discouraged.
<a name="line78"></a> */
<a name="line79"></a>goog.testing.events.Event.prototype.propagationStopped_ = false;
<a name="line80"></a>
<a name="line81"></a>
<a name="line82"></a>/** @override */
<a name="line83"></a>goog.testing.events.Event.prototype.defaultPrevented = false;
<a name="line84"></a>
<a name="line85"></a>
<a name="line86"></a>/**
<a name="line87"></a> * Return value for in internal capture/bubble processing for IE.
<a name="line88"></a> * @type {boolean}
<a name="line89"></a> * @suppress {underscore} Technically public, but referencing this outside
<a name="line90"></a> *     this package is strongly discouraged.
<a name="line91"></a> */
<a name="line92"></a>goog.testing.events.Event.prototype.returnValue_ = true;
<a name="line93"></a>
<a name="line94"></a>
<a name="line95"></a>/** @override */
<a name="line96"></a>goog.testing.events.Event.prototype.stopPropagation = function() {
<a name="line97"></a>  this.propagationStopped_ = true;
<a name="line98"></a>};
<a name="line99"></a>
<a name="line100"></a>
<a name="line101"></a>/** @override */
<a name="line102"></a>goog.testing.events.Event.prototype.preventDefault = function() {
<a name="line103"></a>  this.defaultPrevented = true;
<a name="line104"></a>  this.returnValue_ = false;
<a name="line105"></a>};
<a name="line106"></a>
<a name="line107"></a>
<a name="line108"></a>/**
<a name="line109"></a> * A static helper function that sets the mouse position to the event.
<a name="line110"></a> * @param {Event} event A simulated native event.
<a name="line111"></a> * @param {goog.math.Coordinate=} opt_coords Mouse position. Defaults to event&#39;s
<a name="line112"></a> * target&#39;s position (if available), otherwise (0, 0).
<a name="line113"></a> * @private
<a name="line114"></a> */
<a name="line115"></a>goog.testing.events.setEventClientXY_ = function(event, opt_coords) {
<a name="line116"></a>  if (!opt_coords &amp;&amp; event.target &amp;&amp;
<a name="line117"></a>      event.target.nodeType == goog.dom.NodeType.ELEMENT) {
<a name="line118"></a>    try {
<a name="line119"></a>      opt_coords =
<a name="line120"></a>          goog.style.getClientPosition(/** @type {Element} **/ (event.target));
<a name="line121"></a>    } catch (ex) {
<a name="line122"></a>      // IE sometimes throws if it can&#39;t get the position.
<a name="line123"></a>    }
<a name="line124"></a>  }
<a name="line125"></a>  event.clientX = opt_coords ? opt_coords.x : 0;
<a name="line126"></a>  event.clientY = opt_coords ? opt_coords.y : 0;
<a name="line127"></a>
<a name="line128"></a>  // Pretend the browser window is at (0, 0).
<a name="line129"></a>  event.screenX = event.clientX;
<a name="line130"></a>  event.screenY = event.clientY;
<a name="line131"></a>};
<a name="line132"></a>
<a name="line133"></a>
<a name="line134"></a>/**
<a name="line135"></a> * Simulates a mousedown, mouseup, and then click on the given event target,
<a name="line136"></a> * with the left mouse button.
<a name="line137"></a> * @param {EventTarget} target The target for the event.
<a name="line138"></a> * @param {goog.events.BrowserEvent.MouseButton=} opt_button Mouse button;
<a name="line139"></a> *     defaults to {@code goog.events.BrowserEvent.MouseButton.LEFT}.
<a name="line140"></a> * @param {goog.math.Coordinate=} opt_coords Mouse position. Defaults to event&#39;s
<a name="line141"></a> * target&#39;s position (if available), otherwise (0, 0).
<a name="line142"></a> * @param {Object=} opt_eventProperties Event properties to be mixed into the
<a name="line143"></a> *     BrowserEvent.
<a name="line144"></a> * @return {boolean} The returnValue of the sequence: false if preventDefault()
<a name="line145"></a> *     was called on any of the events, true otherwise.
<a name="line146"></a> */
<a name="line147"></a>goog.testing.events.fireClickSequence =
<a name="line148"></a>    function(target, opt_button, opt_coords, opt_eventProperties) {
<a name="line149"></a>  // Fire mousedown, mouseup, and click. Then return the bitwise AND of the 3.
<a name="line150"></a>  return !!(goog.testing.events.fireMouseDownEvent(
<a name="line151"></a>                target, opt_button, opt_coords, opt_eventProperties) &amp;
<a name="line152"></a>            goog.testing.events.fireMouseUpEvent(
<a name="line153"></a>                target, opt_button, opt_coords, opt_eventProperties) &amp;
<a name="line154"></a>            goog.testing.events.fireClickEvent(
<a name="line155"></a>                target, opt_button, opt_coords, opt_eventProperties));
<a name="line156"></a>};
<a name="line157"></a>
<a name="line158"></a>
<a name="line159"></a>/**
<a name="line160"></a> * Simulates the sequence of events fired by the browser when the user double-
<a name="line161"></a> * clicks the given target.
<a name="line162"></a> * @param {EventTarget} target The target for the event.
<a name="line163"></a> * @param {goog.math.Coordinate=} opt_coords Mouse position. Defaults to event&#39;s
<a name="line164"></a> * target&#39;s position (if available), otherwise (0, 0).
<a name="line165"></a> * @param {Object=} opt_eventProperties Event properties to be mixed into the
<a name="line166"></a> *     BrowserEvent.
<a name="line167"></a> * @return {boolean} The returnValue of the sequence: false if preventDefault()
<a name="line168"></a> *     was called on any of the events, true otherwise.
<a name="line169"></a> */
<a name="line170"></a>goog.testing.events.fireDoubleClickSequence = function(
<a name="line171"></a>    target, opt_coords, opt_eventProperties) {
<a name="line172"></a>  // Fire mousedown, mouseup, click, mousedown, mouseup, click, dblclick.
<a name="line173"></a>  // Then return the bitwise AND of the 7.
<a name="line174"></a>  var btn = goog.events.BrowserEvent.MouseButton.LEFT;
<a name="line175"></a>  return !!(goog.testing.events.fireMouseDownEvent(
<a name="line176"></a>                target, btn, opt_coords, opt_eventProperties) &amp;
<a name="line177"></a>            goog.testing.events.fireMouseUpEvent(
<a name="line178"></a>                target, btn, opt_coords, opt_eventProperties) &amp;
<a name="line179"></a>            goog.testing.events.fireClickEvent(
<a name="line180"></a>                target, btn, opt_coords, opt_eventProperties) &amp;
<a name="line181"></a>            // IE fires a selectstart instead of the second mousedown in a
<a name="line182"></a>            // dblclick, but we don&#39;t care about selectstart.
<a name="line183"></a>            (goog.userAgent.IE ||
<a name="line184"></a>            goog.testing.events.fireMouseDownEvent(
<a name="line185"></a>                target, btn, opt_coords, opt_eventProperties)) &amp;
<a name="line186"></a>            goog.testing.events.fireMouseUpEvent(
<a name="line187"></a>                target, btn, opt_coords, opt_eventProperties) &amp;
<a name="line188"></a>            // IE doesn&#39;t fire the second click in a dblclick.
<a name="line189"></a>            (goog.userAgent.IE ||
<a name="line190"></a>            goog.testing.events.fireClickEvent(
<a name="line191"></a>                target, btn, opt_coords, opt_eventProperties)) &amp;
<a name="line192"></a>            goog.testing.events.fireDoubleClickEvent(
<a name="line193"></a>                target, opt_coords, opt_eventProperties));
<a name="line194"></a>};
<a name="line195"></a>
<a name="line196"></a>
<a name="line197"></a>/**
<a name="line198"></a> * Simulates a complete keystroke (keydown, keypress, and keyup). Note that
<a name="line199"></a> * if preventDefault is called on the keydown, the keypress will not fire.
<a name="line200"></a> *
<a name="line201"></a> * @param {EventTarget} target The target for the event.
<a name="line202"></a> * @param {number} keyCode The keycode of the key pressed.
<a name="line203"></a> * @param {Object=} opt_eventProperties Event properties to be mixed into the
<a name="line204"></a> *     BrowserEvent.
<a name="line205"></a> * @return {boolean} The returnValue of the sequence: false if preventDefault()
<a name="line206"></a> *     was called on any of the events, true otherwise.
<a name="line207"></a> */
<a name="line208"></a>goog.testing.events.fireKeySequence = function(
<a name="line209"></a>    target, keyCode, opt_eventProperties) {
<a name="line210"></a>  return goog.testing.events.fireNonAsciiKeySequence(target, keyCode, keyCode,
<a name="line211"></a>                                                     opt_eventProperties);
<a name="line212"></a>};
<a name="line213"></a>
<a name="line214"></a>
<a name="line215"></a>/**
<a name="line216"></a> * Simulates a complete keystroke (keydown, keypress, and keyup) when typing
<a name="line217"></a> * a non-ASCII character. Same as fireKeySequence, the keypress will not fire
<a name="line218"></a> * if preventDefault is called on the keydown.
<a name="line219"></a> *
<a name="line220"></a> * @param {EventTarget} target The target for the event.
<a name="line221"></a> * @param {number} keyCode The keycode of the keydown and keyup events.
<a name="line222"></a> * @param {number} keyPressKeyCode The keycode of the keypress event.
<a name="line223"></a> * @param {Object=} opt_eventProperties Event properties to be mixed into the
<a name="line224"></a> *     BrowserEvent.
<a name="line225"></a> * @return {boolean} The returnValue of the sequence: false if preventDefault()
<a name="line226"></a> *     was called on any of the events, true otherwise.
<a name="line227"></a> */
<a name="line228"></a>goog.testing.events.fireNonAsciiKeySequence = function(
<a name="line229"></a>    target, keyCode, keyPressKeyCode, opt_eventProperties) {
<a name="line230"></a>  var keydown =
<a name="line231"></a>      new goog.testing.events.Event(goog.events.EventType.KEYDOWN, target);
<a name="line232"></a>  var keyup =
<a name="line233"></a>      new goog.testing.events.Event(goog.events.EventType.KEYUP, target);
<a name="line234"></a>  var keypress =
<a name="line235"></a>      new goog.testing.events.Event(goog.events.EventType.KEYPRESS, target);
<a name="line236"></a>  keydown.keyCode = keyup.keyCode = keyCode;
<a name="line237"></a>  keypress.keyCode = keyPressKeyCode;
<a name="line238"></a>
<a name="line239"></a>  if (opt_eventProperties) {
<a name="line240"></a>    goog.object.extend(keydown, opt_eventProperties);
<a name="line241"></a>    goog.object.extend(keyup, opt_eventProperties);
<a name="line242"></a>    goog.object.extend(keypress, opt_eventProperties);
<a name="line243"></a>  }
<a name="line244"></a>
<a name="line245"></a>  // Fire keydown, keypress, and keyup. Note that if the keydown is
<a name="line246"></a>  // prevent-defaulted, then the keypress will not fire on IE.
<a name="line247"></a>  var result = true;
<a name="line248"></a>  if (!goog.testing.events.isBrokenGeckoMacActionKey_(keydown)) {
<a name="line249"></a>    result = goog.testing.events.fireBrowserEvent(keydown);
<a name="line250"></a>  }
<a name="line251"></a>  if (goog.events.KeyCodes.firesKeyPressEvent(
<a name="line252"></a>          keyCode, undefined, keydown.shiftKey, keydown.ctrlKey,
<a name="line253"></a>          keydown.altKey) &amp;&amp;
<a name="line254"></a>      !(goog.userAgent.IE &amp;&amp; !result)) {
<a name="line255"></a>    result &amp;= goog.testing.events.fireBrowserEvent(keypress);
<a name="line256"></a>  }
<a name="line257"></a>  return !!(result &amp; goog.testing.events.fireBrowserEvent(keyup));
<a name="line258"></a>};
<a name="line259"></a>
<a name="line260"></a>
<a name="line261"></a>/**
<a name="line262"></a> * @param {goog.testing.events.Event} e The event.
<a name="line263"></a> * @return {boolean} Whether this is the Gecko/Mac&#39;s Meta-C/V/X, which
<a name="line264"></a> *     is broken and requires special handling.
<a name="line265"></a> * @private
<a name="line266"></a> */
<a name="line267"></a>goog.testing.events.isBrokenGeckoMacActionKey_ = function(e) {
<a name="line268"></a>  return goog.userAgent.MAC &amp;&amp; goog.userAgent.GECKO &amp;&amp;
<a name="line269"></a>      (e.keyCode == goog.events.KeyCodes.C ||
<a name="line270"></a>       e.keyCode == goog.events.KeyCodes.X ||
<a name="line271"></a>       e.keyCode == goog.events.KeyCodes.V) &amp;&amp; e.metaKey;
<a name="line272"></a>};
<a name="line273"></a>
<a name="line274"></a>
<a name="line275"></a>/**
<a name="line276"></a> * Simulates a mouseover event on the given target.
<a name="line277"></a> * @param {EventTarget} target The target for the event.
<a name="line278"></a> * @param {EventTarget} relatedTarget The related target for the event (e.g.,
<a name="line279"></a> *     the node that the mouse is being moved out of).
<a name="line280"></a> * @param {goog.math.Coordinate=} opt_coords Mouse position. Defaults to event&#39;s
<a name="line281"></a> * target&#39;s position (if available), otherwise (0, 0).
<a name="line282"></a> * @return {boolean} The returnValue of the event: false if preventDefault() was
<a name="line283"></a> *     called on it, true otherwise.
<a name="line284"></a> */
<a name="line285"></a>goog.testing.events.fireMouseOverEvent = function(target, relatedTarget,
<a name="line286"></a>    opt_coords) {
<a name="line287"></a>  var mouseover =
<a name="line288"></a>      new goog.testing.events.Event(goog.events.EventType.MOUSEOVER, target);
<a name="line289"></a>  mouseover.relatedTarget = relatedTarget;
<a name="line290"></a>  goog.testing.events.setEventClientXY_(mouseover, opt_coords);
<a name="line291"></a>  return goog.testing.events.fireBrowserEvent(mouseover);
<a name="line292"></a>};
<a name="line293"></a>
<a name="line294"></a>
<a name="line295"></a>/**
<a name="line296"></a> * Simulates a mousemove event on the given target.
<a name="line297"></a> * @param {EventTarget} target The target for the event.
<a name="line298"></a> * @param {goog.math.Coordinate=} opt_coords Mouse position. Defaults to event&#39;s
<a name="line299"></a> * target&#39;s position (if available), otherwise (0, 0).
<a name="line300"></a> * @return {boolean} The returnValue of the event: false if preventDefault() was
<a name="line301"></a> *     called on it, true otherwise.
<a name="line302"></a> */
<a name="line303"></a>goog.testing.events.fireMouseMoveEvent = function(target, opt_coords) {
<a name="line304"></a>  var mousemove =
<a name="line305"></a>      new goog.testing.events.Event(goog.events.EventType.MOUSEMOVE, target);
<a name="line306"></a>
<a name="line307"></a>  goog.testing.events.setEventClientXY_(mousemove, opt_coords);
<a name="line308"></a>  return goog.testing.events.fireBrowserEvent(mousemove);
<a name="line309"></a>};
<a name="line310"></a>
<a name="line311"></a>
<a name="line312"></a>/**
<a name="line313"></a> * Simulates a mouseout event on the given target.
<a name="line314"></a> * @param {EventTarget} target The target for the event.
<a name="line315"></a> * @param {EventTarget} relatedTarget The related target for the event (e.g.,
<a name="line316"></a> *     the node that the mouse is being moved into).
<a name="line317"></a> * @param {goog.math.Coordinate=} opt_coords Mouse position. Defaults to event&#39;s
<a name="line318"></a> * target&#39;s position (if available), otherwise (0, 0).
<a name="line319"></a> * @return {boolean} The returnValue of the event: false if preventDefault() was
<a name="line320"></a> *     called on it, true otherwise.
<a name="line321"></a> */
<a name="line322"></a>goog.testing.events.fireMouseOutEvent = function(target, relatedTarget,
<a name="line323"></a>    opt_coords) {
<a name="line324"></a>  var mouseout =
<a name="line325"></a>      new goog.testing.events.Event(goog.events.EventType.MOUSEOUT, target);
<a name="line326"></a>  mouseout.relatedTarget = relatedTarget;
<a name="line327"></a>  goog.testing.events.setEventClientXY_(mouseout, opt_coords);
<a name="line328"></a>  return goog.testing.events.fireBrowserEvent(mouseout);
<a name="line329"></a>};
<a name="line330"></a>
<a name="line331"></a>
<a name="line332"></a>/**
<a name="line333"></a> * Simulates a mousedown event on the given target.
<a name="line334"></a> * @param {EventTarget} target The target for the event.
<a name="line335"></a> * @param {goog.events.BrowserEvent.MouseButton=} opt_button Mouse button;
<a name="line336"></a> *     defaults to {@code goog.events.BrowserEvent.MouseButton.LEFT}.
<a name="line337"></a> * @param {goog.math.Coordinate=} opt_coords Mouse position. Defaults to event&#39;s
<a name="line338"></a> * target&#39;s position (if available), otherwise (0, 0).
<a name="line339"></a> * @param {Object=} opt_eventProperties Event properties to be mixed into the
<a name="line340"></a> *     BrowserEvent.
<a name="line341"></a> * @return {boolean} The returnValue of the event: false if preventDefault() was
<a name="line342"></a> *     called on it, true otherwise.
<a name="line343"></a> */
<a name="line344"></a>goog.testing.events.fireMouseDownEvent =
<a name="line345"></a>    function(target, opt_button, opt_coords, opt_eventProperties) {
<a name="line346"></a>
<a name="line347"></a>  var button = opt_button || goog.events.BrowserEvent.MouseButton.LEFT;
<a name="line348"></a>  button = !goog.events.BrowserFeature.HAS_W3C_BUTTON ?
<a name="line349"></a>      goog.events.BrowserEvent.IEButtonMap[button] : button;
<a name="line350"></a>  return goog.testing.events.fireMouseButtonEvent_(
<a name="line351"></a>      goog.events.EventType.MOUSEDOWN, target, button, opt_coords,
<a name="line352"></a>      opt_eventProperties);
<a name="line353"></a>};
<a name="line354"></a>
<a name="line355"></a>
<a name="line356"></a>/**
<a name="line357"></a> * Simulates a mouseup event on the given target.
<a name="line358"></a> * @param {EventTarget} target The target for the event.
<a name="line359"></a> * @param {goog.events.BrowserEvent.MouseButton=} opt_button Mouse button;
<a name="line360"></a> *     defaults to {@code goog.events.BrowserEvent.MouseButton.LEFT}.
<a name="line361"></a> * @param {goog.math.Coordinate=} opt_coords Mouse position. Defaults to event&#39;s
<a name="line362"></a> * target&#39;s position (if available), otherwise (0, 0).
<a name="line363"></a> * @param {Object=} opt_eventProperties Event properties to be mixed into the
<a name="line364"></a> *     BrowserEvent.
<a name="line365"></a> * @return {boolean} The returnValue of the event: false if preventDefault() was
<a name="line366"></a> *     called on it, true otherwise.
<a name="line367"></a> */
<a name="line368"></a>goog.testing.events.fireMouseUpEvent =
<a name="line369"></a>    function(target, opt_button, opt_coords, opt_eventProperties) {
<a name="line370"></a>  var button = opt_button || goog.events.BrowserEvent.MouseButton.LEFT;
<a name="line371"></a>  button = !goog.events.BrowserFeature.HAS_W3C_BUTTON ?
<a name="line372"></a>      goog.events.BrowserEvent.IEButtonMap[button] : button;
<a name="line373"></a>  return goog.testing.events.fireMouseButtonEvent_(
<a name="line374"></a>      goog.events.EventType.MOUSEUP, target, button, opt_coords,
<a name="line375"></a>      opt_eventProperties);
<a name="line376"></a>};
<a name="line377"></a>
<a name="line378"></a>
<a name="line379"></a>/**
<a name="line380"></a> * Simulates a click event on the given target. IE only supports click with
<a name="line381"></a> * the left mouse button.
<a name="line382"></a> * @param {EventTarget} target The target for the event.
<a name="line383"></a> * @param {goog.events.BrowserEvent.MouseButton=} opt_button Mouse button;
<a name="line384"></a> *     defaults to {@code goog.events.BrowserEvent.MouseButton.LEFT}.
<a name="line385"></a> * @param {goog.math.Coordinate=} opt_coords Mouse position. Defaults to event&#39;s
<a name="line386"></a> * target&#39;s position (if available), otherwise (0, 0).
<a name="line387"></a> * @param {Object=} opt_eventProperties Event properties to be mixed into the
<a name="line388"></a> *     BrowserEvent.
<a name="line389"></a> * @return {boolean} The returnValue of the event: false if preventDefault() was
<a name="line390"></a> *     called on it, true otherwise.
<a name="line391"></a> */
<a name="line392"></a>goog.testing.events.fireClickEvent =
<a name="line393"></a>    function(target, opt_button, opt_coords, opt_eventProperties) {
<a name="line394"></a>  return goog.testing.events.fireMouseButtonEvent_(goog.events.EventType.CLICK,
<a name="line395"></a>      target, opt_button, opt_coords, opt_eventProperties);
<a name="line396"></a>};
<a name="line397"></a>
<a name="line398"></a>
<a name="line399"></a>/**
<a name="line400"></a> * Simulates a double-click event on the given target. Always double-clicks
<a name="line401"></a> * with the left mouse button since no browser supports double-clicking with
<a name="line402"></a> * any other buttons.
<a name="line403"></a> * @param {EventTarget} target The target for the event.
<a name="line404"></a> * @param {goog.math.Coordinate=} opt_coords Mouse position. Defaults to event&#39;s
<a name="line405"></a> * target&#39;s position (if available), otherwise (0, 0).
<a name="line406"></a> * @param {Object=} opt_eventProperties Event properties to be mixed into the
<a name="line407"></a> *     BrowserEvent.
<a name="line408"></a> * @return {boolean} The returnValue of the event: false if preventDefault() was
<a name="line409"></a> *     called on it, true otherwise.
<a name="line410"></a> */
<a name="line411"></a>goog.testing.events.fireDoubleClickEvent =
<a name="line412"></a>    function(target, opt_coords, opt_eventProperties) {
<a name="line413"></a>  return goog.testing.events.fireMouseButtonEvent_(
<a name="line414"></a>      goog.events.EventType.DBLCLICK, target,
<a name="line415"></a>      goog.events.BrowserEvent.MouseButton.LEFT, opt_coords,
<a name="line416"></a>      opt_eventProperties);
<a name="line417"></a>};
<a name="line418"></a>
<a name="line419"></a>
<a name="line420"></a>/**
<a name="line421"></a> * Helper function to fire a mouse event.
<a name="line422"></a> * with the left mouse button since no browser supports double-clicking with
<a name="line423"></a> * any other buttons.
<a name="line424"></a> * @param {string} type The event type.
<a name="line425"></a> * @param {EventTarget} target The target for the event.
<a name="line426"></a> * @param {number=} opt_button Mouse button; defaults to
<a name="line427"></a> *     {@code goog.events.BrowserEvent.MouseButton.LEFT}.
<a name="line428"></a> * @param {goog.math.Coordinate=} opt_coords Mouse position. Defaults to event&#39;s
<a name="line429"></a> * target&#39;s position (if available), otherwise (0, 0).
<a name="line430"></a> * @param {Object=} opt_eventProperties Event properties to be mixed into the
<a name="line431"></a> *     BrowserEvent.
<a name="line432"></a> * @return {boolean} The returnValue of the event: false if preventDefault() was
<a name="line433"></a> *     called on it, true otherwise.
<a name="line434"></a> * @private
<a name="line435"></a> */
<a name="line436"></a>goog.testing.events.fireMouseButtonEvent_ =
<a name="line437"></a>    function(type, target, opt_button, opt_coords, opt_eventProperties) {
<a name="line438"></a>  var e =
<a name="line439"></a>      new goog.testing.events.Event(type, target);
<a name="line440"></a>  e.button = opt_button || goog.events.BrowserEvent.MouseButton.LEFT;
<a name="line441"></a>  goog.testing.events.setEventClientXY_(e, opt_coords);
<a name="line442"></a>  if (opt_eventProperties) {
<a name="line443"></a>    goog.object.extend(e, opt_eventProperties);
<a name="line444"></a>  }
<a name="line445"></a>  return goog.testing.events.fireBrowserEvent(e);
<a name="line446"></a>};
<a name="line447"></a>
<a name="line448"></a>
<a name="line449"></a>/**
<a name="line450"></a> * Simulates a contextmenu event on the given target.
<a name="line451"></a> * @param {EventTarget} target The target for the event.
<a name="line452"></a> * @param {goog.math.Coordinate=} opt_coords Mouse position. Defaults to event&#39;s
<a name="line453"></a> * target&#39;s position (if available), otherwise (0, 0).
<a name="line454"></a> * @return {boolean} The returnValue of the event: false if preventDefault() was
<a name="line455"></a> *     called on it, true otherwise.
<a name="line456"></a> */
<a name="line457"></a>goog.testing.events.fireContextMenuEvent = function(target, opt_coords) {
<a name="line458"></a>  var button = (goog.userAgent.MAC &amp;&amp; goog.userAgent.WEBKIT) ?
<a name="line459"></a>      goog.events.BrowserEvent.MouseButton.LEFT :
<a name="line460"></a>      goog.events.BrowserEvent.MouseButton.RIGHT;
<a name="line461"></a>  var contextmenu =
<a name="line462"></a>      new goog.testing.events.Event(goog.events.EventType.CONTEXTMENU, target);
<a name="line463"></a>  contextmenu.button = !goog.events.BrowserFeature.HAS_W3C_BUTTON ?
<a name="line464"></a>      goog.events.BrowserEvent.IEButtonMap[button] : button;
<a name="line465"></a>  contextmenu.ctrlKey = goog.userAgent.MAC;
<a name="line466"></a>  goog.testing.events.setEventClientXY_(contextmenu, opt_coords);
<a name="line467"></a>  return goog.testing.events.fireBrowserEvent(contextmenu);
<a name="line468"></a>};
<a name="line469"></a>
<a name="line470"></a>
<a name="line471"></a>/**
<a name="line472"></a> * Simulates a mousedown, contextmenu, and the mouseup on the given event
<a name="line473"></a> * target, with the right mouse button.
<a name="line474"></a> * @param {EventTarget} target The target for the event.
<a name="line475"></a> * @param {goog.math.Coordinate=} opt_coords Mouse position. Defaults to event&#39;s
<a name="line476"></a> * target&#39;s position (if available), otherwise (0, 0).
<a name="line477"></a> * @return {boolean} The returnValue of the sequence: false if preventDefault()
<a name="line478"></a> *     was called on any of the events, true otherwise.
<a name="line479"></a> */
<a name="line480"></a>goog.testing.events.fireContextMenuSequence = function(target, opt_coords) {
<a name="line481"></a>  var props = goog.userAgent.MAC ? {ctrlKey: true} : {};
<a name="line482"></a>  var button = (goog.userAgent.MAC &amp;&amp; goog.userAgent.WEBKIT) ?
<a name="line483"></a>      goog.events.BrowserEvent.MouseButton.LEFT :
<a name="line484"></a>      goog.events.BrowserEvent.MouseButton.RIGHT;
<a name="line485"></a>
<a name="line486"></a>  var result = goog.testing.events.fireMouseDownEvent(target,
<a name="line487"></a>      button, opt_coords, props);
<a name="line488"></a>  if (goog.userAgent.WINDOWS) {
<a name="line489"></a>    // All browsers are consistent on Windows.
<a name="line490"></a>    result &amp;= goog.testing.events.fireMouseUpEvent(target,
<a name="line491"></a>                  button, opt_coords) &amp;
<a name="line492"></a>              goog.testing.events.fireContextMenuEvent(target, opt_coords);
<a name="line493"></a>  } else {
<a name="line494"></a>    result &amp;= goog.testing.events.fireContextMenuEvent(target, opt_coords);
<a name="line495"></a>
<a name="line496"></a>    // GECKO on Mac and Linux always fires the mouseup after the contextmenu.
<a name="line497"></a>
<a name="line498"></a>    // WEBKIT is really weird.
<a name="line499"></a>    //
<a name="line500"></a>    // On Linux, it sometimes fires mouseup, but most of the time doesn&#39;t.
<a name="line501"></a>    // It&#39;s really hard to reproduce consistently. I think there&#39;s some
<a name="line502"></a>    // internal race condition. If contextmenu is preventDefaulted, then
<a name="line503"></a>    // mouseup always fires.
<a name="line504"></a>    //
<a name="line505"></a>    // On Mac, it always fires mouseup and then fires a click.
<a name="line506"></a>    result &amp;= goog.testing.events.fireMouseUpEvent(target,
<a name="line507"></a>        button, opt_coords, props);
<a name="line508"></a>
<a name="line509"></a>    if (goog.userAgent.WEBKIT &amp;&amp; goog.userAgent.MAC) {
<a name="line510"></a>      result &amp;= goog.testing.events.fireClickEvent(
<a name="line511"></a>          target, button, opt_coords, props);
<a name="line512"></a>    }
<a name="line513"></a>  }
<a name="line514"></a>  return !!result;
<a name="line515"></a>};
<a name="line516"></a>
<a name="line517"></a>
<a name="line518"></a>/**
<a name="line519"></a> * Simulates a popstate event on the given target.
<a name="line520"></a> * @param {EventTarget} target The target for the event.
<a name="line521"></a> * @param {Object} state History state object.
<a name="line522"></a> * @return {boolean} The returnValue of the event: false if preventDefault() was
<a name="line523"></a> *     called on it, true otherwise.
<a name="line524"></a> */
<a name="line525"></a>goog.testing.events.firePopStateEvent = function(target, state) {
<a name="line526"></a>  var e = new goog.testing.events.Event(goog.events.EventType.POPSTATE, target);
<a name="line527"></a>  e.state = state;
<a name="line528"></a>  return goog.testing.events.fireBrowserEvent(e);
<a name="line529"></a>};
<a name="line530"></a>
<a name="line531"></a>
<a name="line532"></a>/**
<a name="line533"></a> * Simulate a focus event on the given target.
<a name="line534"></a> * @param {EventTarget} target The target for the event.
<a name="line535"></a> * @return {boolean} The value returned by firing the focus browser event,
<a name="line536"></a> *     which returns false iff &#39;preventDefault&#39; was invoked.
<a name="line537"></a> */
<a name="line538"></a>goog.testing.events.fireFocusEvent = function(target) {
<a name="line539"></a>  var e = new goog.testing.events.Event(
<a name="line540"></a>      goog.events.EventType.FOCUS, target);
<a name="line541"></a>  return goog.testing.events.fireBrowserEvent(e);
<a name="line542"></a>};
<a name="line543"></a>
<a name="line544"></a>
<a name="line545"></a>/**
<a name="line546"></a> * Simulates an event&#39;s capturing and bubbling phases.
<a name="line547"></a> * @param {Event} event A simulated native event. It will be wrapped in a
<a name="line548"></a> *     normalized BrowserEvent and dispatched to Closure listeners on all
<a name="line549"></a> *     ancestors of its target (inclusive).
<a name="line550"></a> * @return {boolean} The returnValue of the event: false if preventDefault() was
<a name="line551"></a> *     called on it, true otherwise.
<a name="line552"></a> */
<a name="line553"></a>goog.testing.events.fireBrowserEvent = function(event) {
<a name="line554"></a>  event.returnValue_ = true;
<a name="line555"></a>
<a name="line556"></a>  // generate a list of ancestors
<a name="line557"></a>  var ancestors = [];
<a name="line558"></a>  for (var current = event.target; current; current = current.parentNode) {
<a name="line559"></a>    ancestors.push(current);
<a name="line560"></a>  }
<a name="line561"></a>
<a name="line562"></a>  // dispatch capturing listeners
<a name="line563"></a>  for (var j = ancestors.length - 1;
<a name="line564"></a>       j &gt;= 0 &amp;&amp; !event.propagationStopped_;
<a name="line565"></a>       j--) {
<a name="line566"></a>    goog.events.fireListeners(ancestors[j], event.type, true,
<a name="line567"></a>        new goog.events.BrowserEvent(event, ancestors[j]));
<a name="line568"></a>  }
<a name="line569"></a>
<a name="line570"></a>  // dispatch bubbling listeners
<a name="line571"></a>  for (var j = 0;
<a name="line572"></a>       j &lt; ancestors.length &amp;&amp; !event.propagationStopped_;
<a name="line573"></a>       j++) {
<a name="line574"></a>    goog.events.fireListeners(ancestors[j], event.type, false,
<a name="line575"></a>        new goog.events.BrowserEvent(event, ancestors[j]));
<a name="line576"></a>  }
<a name="line577"></a>
<a name="line578"></a>  return event.returnValue_;
<a name="line579"></a>};
<a name="line580"></a>
<a name="line581"></a>
<a name="line582"></a>/**
<a name="line583"></a> * Simulates a touchstart event on the given target.
<a name="line584"></a> * @param {EventTarget} target The target for the event.
<a name="line585"></a> * @param {goog.math.Coordinate=} opt_coords Touch position. Defaults to event&#39;s
<a name="line586"></a> *     target&#39;s position (if available), otherwise (0, 0).
<a name="line587"></a> * @param {Object=} opt_eventProperties Event properties to be mixed into the
<a name="line588"></a> *     BrowserEvent.
<a name="line589"></a> * @return {boolean} The returnValue of the event: false if preventDefault() was
<a name="line590"></a> *     called on it, true otherwise.
<a name="line591"></a> */
<a name="line592"></a>goog.testing.events.fireTouchStartEvent = function(
<a name="line593"></a>    target, opt_coords, opt_eventProperties) {
<a name="line594"></a>  // TODO: Support multi-touch events with array of coordinates.
<a name="line595"></a>  var touchstart =
<a name="line596"></a>      new goog.testing.events.Event(goog.events.EventType.TOUCHSTART, target);
<a name="line597"></a>  goog.testing.events.setEventClientXY_(touchstart, opt_coords);
<a name="line598"></a>  return goog.testing.events.fireBrowserEvent(touchstart);
<a name="line599"></a>};
<a name="line600"></a>
<a name="line601"></a>
<a name="line602"></a>/**
<a name="line603"></a> * Simulates a touchmove event on the given target.
<a name="line604"></a> * @param {EventTarget} target The target for the event.
<a name="line605"></a> * @param {goog.math.Coordinate=} opt_coords Touch position. Defaults to event&#39;s
<a name="line606"></a> *     target&#39;s position (if available), otherwise (0, 0).
<a name="line607"></a> * @param {Object=} opt_eventProperties Event properties to be mixed into the
<a name="line608"></a> *     BrowserEvent.
<a name="line609"></a> * @return {boolean} The returnValue of the event: false if preventDefault() was
<a name="line610"></a> *     called on it, true otherwise.
<a name="line611"></a> */
<a name="line612"></a>goog.testing.events.fireTouchMoveEvent = function(
<a name="line613"></a>    target, opt_coords, opt_eventProperties) {
<a name="line614"></a>  // TODO: Support multi-touch events with array of coordinates.
<a name="line615"></a>  var touchmove =
<a name="line616"></a>      new goog.testing.events.Event(goog.events.EventType.TOUCHMOVE, target);
<a name="line617"></a>  goog.testing.events.setEventClientXY_(touchmove, opt_coords);
<a name="line618"></a>  return goog.testing.events.fireBrowserEvent(touchmove);
<a name="line619"></a>};
<a name="line620"></a>
<a name="line621"></a>
<a name="line622"></a>/**
<a name="line623"></a> * Simulates a touchend event on the given target.
<a name="line624"></a> * @param {EventTarget} target The target for the event.
<a name="line625"></a> * @param {goog.math.Coordinate=} opt_coords Touch position. Defaults to event&#39;s
<a name="line626"></a> *     target&#39;s position (if available), otherwise (0, 0).
<a name="line627"></a> * @param {Object=} opt_eventProperties Event properties to be mixed into the
<a name="line628"></a> *     BrowserEvent.
<a name="line629"></a> * @return {boolean} The returnValue of the event: false if preventDefault() was
<a name="line630"></a> *     called on it, true otherwise.
<a name="line631"></a> */
<a name="line632"></a>goog.testing.events.fireTouchEndEvent = function(
<a name="line633"></a>    target, opt_coords, opt_eventProperties) {
<a name="line634"></a>  // TODO: Support multi-touch events with array of coordinates.
<a name="line635"></a>  var touchend =
<a name="line636"></a>      new goog.testing.events.Event(goog.events.EventType.TOUCHEND, target);
<a name="line637"></a>  goog.testing.events.setEventClientXY_(touchend, opt_coords);
<a name="line638"></a>  return goog.testing.events.fireBrowserEvent(touchend);
<a name="line639"></a>};
<a name="line640"></a>
<a name="line641"></a>
<a name="line642"></a>/**
<a name="line643"></a> * Simulates a simple touch sequence on the given target.
<a name="line644"></a> * @param {EventTarget} target The target for the event.
<a name="line645"></a> * @param {goog.math.Coordinate=} opt_coords Touch position. Defaults to event
<a name="line646"></a> *     target&#39;s position (if available), otherwise (0, 0).
<a name="line647"></a> * @param {Object=} opt_eventProperties Event properties to be mixed into the
<a name="line648"></a> *     BrowserEvent.
<a name="line649"></a> * @return {boolean} The returnValue of the sequence: false if preventDefault()
<a name="line650"></a> *     was called on any of the events, true otherwise.
<a name="line651"></a> */
<a name="line652"></a>goog.testing.events.fireTouchSequence = function(
<a name="line653"></a>    target, opt_coords, opt_eventProperties) {
<a name="line654"></a>  // TODO: Support multi-touch events with array of coordinates.
<a name="line655"></a>  // Fire touchstart, touchmove, touchend then return the bitwise AND of the 3.
<a name="line656"></a>  return !!(goog.testing.events.fireTouchStartEvent(
<a name="line657"></a>                target, opt_coords, opt_eventProperties) &amp;
<a name="line658"></a>            goog.testing.events.fireTouchEndEvent(
<a name="line659"></a>                target, opt_coords, opt_eventProperties));
<a name="line660"></a>};
<a name="line661"></a>
<a name="line662"></a>
<a name="line663"></a>/**
<a name="line664"></a> * Mixins a listenable into the given object. This turns the object
<a name="line665"></a> * into a goog.events.Listenable. This is useful, for example, when
<a name="line666"></a> * you need to mock a implementation of listenable and still want it
<a name="line667"></a> * to work with goog.events.
<a name="line668"></a> * @param {!Object} obj The object to mixin into.
<a name="line669"></a> */
<a name="line670"></a>goog.testing.events.mixinListenable = function(obj) {
<a name="line671"></a>  var listenable = new goog.events.EventTarget();
<a name="line672"></a>
<a name="line673"></a>  if (goog.events.Listenable.USE_LISTENABLE_INTERFACE) {
<a name="line674"></a>    listenable.setTargetForTesting(obj);
<a name="line675"></a>
<a name="line676"></a>    var listenablePrototype = goog.events.EventTarget.prototype;
<a name="line677"></a>    var disposablePrototype = goog.Disposable.prototype;
<a name="line678"></a>    for (var key in listenablePrototype) {
<a name="line679"></a>      if (listenablePrototype.hasOwnProperty(key) ||
<a name="line680"></a>          disposablePrototype.hasOwnProperty(key)) {
<a name="line681"></a>        var member = listenablePrototype[key];
<a name="line682"></a>        if (goog.isFunction(member)) {
<a name="line683"></a>          obj[key] = goog.bind(member, listenable);
<a name="line684"></a>        } else {
<a name="line685"></a>          obj[key] = member;
<a name="line686"></a>        }
<a name="line687"></a>      }
<a name="line688"></a>    }
<a name="line689"></a>  } else {
<a name="line690"></a>    goog.mixin(obj, listenable);
<a name="line691"></a>  }
<a name="line692"></a>};
</pre>


</body>
</html>
